package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

//九宫格计算器
 public class MouseKeyEvent extends JFrame implements KeyListener, MouseListener {
   private JLabel lbl;
    private JTextArea ta;
    private JButton[][] btn;
    private JButton reset;
    private static String[] key={"123","456","789","*0#"};
    private Font ft1,ft2,ft3;
    public MouseKeyEvent(){
        setLayout(null);
        ft1=new Font("宋体",Font.BOLD,20);
        ft2=new Font("Times New Roman",Font.BOLD,20);
        ft3=new Font("Times New Roman",Font.BOLD,30);
        lbl=new JLabel("输入信息");
        lbl.setFont(ft1);
        lbl.setBounds(20,10,180,20);
        ta=new JTextArea();
        ta.setFont(ft2);
        ta.setBounds(20,30,180,50);
        ta.setLineWrap(true);
        ta.setEditable(false);
        btn=new JButton[key.length][];
        for(int i=0;i<key.length;i++){
            btn[i]=new JButton[key[i].length()];
            for(int j=0;j<key[i].length();j++){
                btn[i][j]=new JButton(String.valueOf(key[i].charAt(j)));
                btn[i][j].setBounds(20+j*65,90+i*60,50,50);
                btn[i][j].setFont(ft3);
                btn[i][j].setBackground(Color.WHITE);
                add(btn[i][j]);
                btn[i][j].addKeyListener(this);
                btn[i][j].addMouseListener(this);
            }
        }
        reset=new JButton("重置");
        reset.setBounds(60,325,100,30);
        reset.addMouseListener(this);
        add(lbl);
        add(ta);
        add(reset);
        setSize(230,400);
        setVisible(true);
    }
    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        lbl.setText("键盘按键输入数字");
        for (int i = 0; i < key.length; i++) {
            for(int j=0;j<key[i].length();j++){
                if(btn[i][j].getText().equals(String.valueOf(
                        Character.toUpperCase(e.getKeyCode())))){
                    btn[i][j].setBackground(Color.red);
                    ta.append(btn[i][j].getText());
                }
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        for (int i = 0; i < key.length; i++) {
            for (int j = 0; j < key[i].length(); j++) {
                if(btn[i][j].getText().equals(String.valueOf(
                        Character.toUpperCase(e.getKeyCode())))){
                    btn[i][j].setBackground(Color.WHITE);
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        lbl.setText("鼠标点击输入数字");
        if(e.getSource().equals(reset)){
            lbl.setText("请输入数字");
            ta.setText("");
        }else{
            JButton button=(JButton) e.getSource();
            ta.append(button.getText());
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

     }

     @Override
     public void mouseEntered(MouseEvent e) {
         JButton button=(JButton) e.getSource();
         for (int i = 0; i < key.length; i++) {
             for (int j = 0; j < key[i].length(); j++) {
                 if(btn[i][j].getText().equals(button.getText()))
                     btn[i][j].setBackground(Color.red);
             }
         }
     }

     @Override
     public void mouseExited(MouseEvent e) {
         JButton button=(JButton) e.getSource();
         for (int i = 0; i < key.length; i++) {
             for (int j = 0; j < key[i].length(); j++) {
                 if(btn[i][j].getText().equals(button.getText()))
                     btn[i][j].setBackground(Color.WHITE);
             }
         }

     }

     public static void main(String[] args) {
         new MouseKey();
     }
 }